#-*- coding: UTF-8 -*-

import re
from diceboy import *
def unescape(text):
    """Removes HTML or XML character references 
       and entities from a text string.
    from Fredrik Lundh
    http://effbot.org/zone/re-sub.htm#unescape-html
    """
    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            # character reference
            try:
                if text[:3] == "&#x":
                    return unichr(int(text[3:-1], 16))
                else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text # leave as is
    return re.sub("&#?\w+;", fixup, text)

def is_reply(reply_string):
    if reply_string == '0':
        reply_string = None
    return reply_string

def input_msg(msg, hint = None):
    if not msg:
        msg = raw_input(hint)
    msg = msg.decode('utf-8')
    while len(msg) > 140:
        hint = '你输入了' + str(len(msg)) + '个字符，请控制在140个字符以内：'
        msg = raw_input(hint)
        msg = msg.decode('utf-8')
    return msg
def input_index(index, limit, hint = None):
    if not index :
            index = raw_input(hint)
    while True:
        while not index.isdigit():
            index = raw_input(u'请用序号（数字）来指定具体地嘀咕消息：')
        index = int(index)
    
        if not 1 <= index <= limit:
            index = raw_input(u'请输入一个有效的序号：')
            continue
        else:
            break
    return int(index)
 


